/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.dao.hibernate;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import edu.emory.mathcs.backport.java.util.Arrays;

import kiff.dao.ForumDAO;
import kiff.entity.Forum;
import kiff.enumerator.ForumStatus;
import kiff.exception.DataLayerException;
import kiff.util.Parameter;

/**
 * A Hibernate implementation of the ForumDAO interface.
 * @author Adam
 * @version $Id: ForumHibernateDAO.java 73 2008-12-02 06:28:25Z a.ruggles $
 */
public final class ForumHibernateDAO extends AbstractHibernateDAO<Forum> implements ForumDAO {

	/**
	 * {@inheritDoc}
	 * @see kiff.dao.ForumDAO#findByStatus(kiff.enumerator.ForumStatus)
	 */
	public List<Forum> findByStatus(final ForumStatus... statuses) throws DataLayerException {
		return find(ForumDAO.FIND_BY_STATUS, Parameter.create("statuses", Arrays.asList(statuses)));
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.dao.ForumDAO#findCountByParent(kiff.entity.Forum)
	 */
	public int findCountByParent(final Forum parent) throws DataLayerException {
		if (parent == null) {
			return findCount(DetachedCriteria.forClass(getPersistentClass())
				 	.add(Restrictions.isNull("parent")));
		} else {
			return findCount(DetachedCriteria.forClass(getPersistentClass())
				 	.add(Restrictions.eq("parent.id", parent.getId())));
		}
	}
}
